package com.ls.mysql.config;

import com.ls.mysql.filter.ADomain;
import com.ls.mysql.filter.BUnLogin;
import com.ls.mysql.filter.CToken;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Maple.Chen
 * @Description 替代springmvc
 * @Date 9:29 2020/5/16
 **/
@Configuration
public class MVC_Config implements WebMvcConfigurer {

    // 视图解析
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        /*把向main的请求转发给*/
//        registry.addViewController("/main").setViewName("index.html");
    }

    // 拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 拦截所有请求，通过判断是否有 @LoginRequired 注解 决定是否需要登录
        /*
        跨域处理拦截器
         */
        registry.addInterceptor(A()).addPathPatterns("/**");
        /*
        登录拦截器
         */
        List list = new ArrayList();
        list.add("/");
        list.add("/home.html");
        list.add("/index.html");
        list.add("/register.html");
        list.add("/js/**");
        list.add("/css/**");
        list.add("/img/**");
        list.add("/*/uniqueUsername");
        list.add("/CheckCode");
        list.add("/*/register");
        list.add("/*/login*");
        list.add("/user/md5login");
        // webjars
        list.add("/webjars/**");
        // 测试阶段放行：
        list.add("/layui/**");
        list.add("/vue/**");
        list.add("/Main*.html");
        list.add("/error");
        list.add("/find");
        //校验token,测试，放行测试接口
        list.add("/test/**");
        list.add("/quartz/**");
        registry.addInterceptor(B())
                .addPathPatterns("/**")
                .excludePathPatterns(list);
        /*
        3、指定接口（token令牌）拦截器
         */
        List list2 = new ArrayList();
        list2.add("/test/login");
        registry.addInterceptor(C())
                .addPathPatterns("/test/**")
                .excludePathPatterns(list2);

    }

    // 静态资源放行
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/statics/**").addResourceLocations("classpath:/statics/");
        registry.addResourceHandler("/templates/**").addResourceLocations("classpath:/templates/");
    }

    @Bean
    public ADomain A() {
        return new ADomain();
    }

    @Bean
    public BUnLogin B() {
        return new BUnLogin();
    }

    @Bean
    public CToken C() {
        return new CToken();
    }

}
